﻿#region LGPL Header
// Copyright (C) 2010, Jackie Ng
// https://github.com/jumpinjackie/fdotoolbox, jumpinjackie@gmail.com
// 
// This library is free software; you can redistribute it and/or
// modify it under the terms of the GNU Lesser General Public
// License as published by the Free Software Foundation; either
// version 2.1 of the License, or (at your option) any later version.
// 
// This library is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
// Lesser General Public License for more details.
// 
// You should have received a copy of the GNU Lesser General Public
// License along with this library; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
// 
//
// See license.txt for more/additional licensing information
#endregion
using System;
using System.Windows.Forms;

namespace FdoToolbox.DataStoreManager.Controls.SchemaDesigner
{
    public partial class DataPropertyCtrl : UserControl
    {
        public DataPropertyCtrl()
        {
            InitializeComponent();
            //Just set these to maximum allowed values, because FDO capabilities
            //doesn't even get these right
            numLength.Minimum = decimal.MinValue;
            numLength.Maximum = decimal.MaxValue;
            numPrecision.Minimum = decimal.MinValue;
            numPrecision.Maximum = decimal.MaxValue;
            numScale.Minimum = decimal.MinValue;
            numScale.Maximum = decimal.MaxValue;
        }

        private SchemaDesignContext _context;

        private DataPropertyDefinitionDecorator _dp;

        public DataPropertyCtrl(DataPropertyDefinitionDecorator p, SchemaDesignContext context, NodeUpdateHandler updater)
            : this()
        {
            _context = context;
            _dp = p;

            txtName.DataBindings.Add("Text", p, "Name");
            txtDescription.DataBindings.Add("Text", p, "Description");

            cmbDataType.DataSource = _context.SupportedDataTypes;
            cmbDataType.DataBindings.Add("SelectedItem", p, "DataType");

            chkReadOnly.DataBindings.Add("Checked", p, "ReadOnly");
            
            numLength.DataBindings.Add("Value", p, "Length");
            numPrecision.DataBindings.Add("Value", p, "Precision");
            numScale.DataBindings.Add("Value", p, "Scale");

            chkAutogenerated.Checked = p.IsAutoGenerated;
            chkAutogenerated.CheckedChanged += (s, e) =>
            {
                p.IsAutoGenerated = chkAutogenerated.Checked;
                if (p.IsAutoGenerated)
                {
                    //Not databound, so we have to modfiy UI state
                    chkNullable.Checked = false;
                }
            };

            chkNullable.Checked = p.Nullable;
            chkNullable.CheckedChanged += (s, e) =>
            {
                p.Nullable = chkNullable.Checked;
                if (p.Nullable)
                {
                    //Not databound, so we have to modfiy UI state
                    chkAutogenerated.Checked = false;
                }
            };

            p.PropertyChanged += (s, evt) =>
            {
                if (evt.PropertyName == "Name")
                    updater();
            };

            chkNullable.Enabled = !p.IsIdentity();

            if (p.ValueConstraint != null)
                txtValueConstraint.Text = p.ValueConstraint.ToString();

            btnEditConstraint.Enabled = _context.SupportsValueConstraints;
        }

        private void cmbDataType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if (cmbDataType.SelectedItem != null)
            {
                var dt = (OSGeo.FDO.Schema.DataType)cmbDataType.SelectedItem;

                //Fix auto-generation
                bool supported = (Array.IndexOf(_context.SupportedAutogeneratedDataTypes, dt) >= 0);
                chkAutogenerated.Enabled = supported;
                if (!supported)
                    chkAutogenerated.Checked = false;

                numLength.Enabled = false;
                numPrecision.Enabled = false;
                numScale.Enabled = false;

                if (dt == OSGeo.FDO.Schema.DataType.DataType_String)
                {
                    numLength.Enabled = true;
                    numPrecision.Enabled = false;
                    numScale.Enabled = false;
                }
                else if (dt == OSGeo.FDO.Schema.DataType.DataType_Decimal)
                {
                    numLength.Enabled = false;
                    numPrecision.Enabled = true;
                    numScale.Enabled = true;
                }
            }
        }

        private void btnEditConstraint_Click(object sender, EventArgs e)
        {
            if (_context.IsConnected)
            {
                var constraint = _dp.ValueConstraint;
                if (constraint == null)
                {
                    constraint = ValueConstraintDialog.GetConstraint(_context.Connection);
                    if (constraint != null)
                        _dp.ValueConstraint = constraint;
                }
                else
                {
                    constraint = ValueConstraintDialog.GetConstraint(constraint, _context.Connection);
                    if (constraint != null)
                        _dp.ValueConstraint = constraint;
                }
            }
            else
            {
                var constraint = _dp.ValueConstraint;
                if (constraint == null)
                {
                    constraint = ValueConstraintDialog.GetConstraint();
                    if (constraint != null)
                        _dp.ValueConstraint = constraint;
                }
                else
                {
                    constraint = ValueConstraintDialog.GetConstraint(constraint);
                    if (constraint != null)
                        _dp.ValueConstraint = constraint;
                }
            }
        }
    }
}
